﻿using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Marbles.Core.Web.Resources;
using System.Drawing;

namespace Marbles.Core.Web.Controls.WebParts
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Text runat=server></{0}:Text>")]
    public class HeaderText : WebPart
    {
        public string Text
        {
            get
            {
                object obj = ViewState["Text"];
                if (obj == null) { return string.Empty; }
                return ((string)obj);
            }
            set { ViewState["Text"] = value; }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Images.HeaderTextImageHandler.RegisterHttpHandler("HeaderText.axd", "Marbles.Core.Web.Images.HeaderTextImageHandler", "GET,HEAD");

        }

        protected override void Render(HtmlTextWriter writer)
        {
            LosFormatter fmnt = new LosFormatter();
            HeaderTextSettings settings = new HeaderTextSettings();

            settings.Text = this.Text;
            settings.Font = string.Format("{0},{1},{2},{3},{4},{5}", this.Font.Italic, this.Font.Bold, this.Font.Strikeout, this.Font.Underline, this.Font.Size.Unit.Value.ToString(), this.Font.Name);

            if (!this.ForeColor.IsEmpty)
            {
                settings.Fc = ColorTranslator.ToHtml(this.ForeColor);
            }

            if (!this.BackColor.IsEmpty)
            {
                settings.Bc = ColorTranslator.ToHtml(this.BackColor);
            }

            StringWriter sw = new StringWriter();
            fmnt.Serialize(sw, settings);
            writer.AddAttribute("alt", this.Text);
            writer.AddAttribute("src", string.Format("HeaderText.axd?s={0}", sw.ToString()));
            writer.AddAttribute("border", "0");
            writer.RenderBeginTag("img");
            writer.RenderEndTag();
        }
    }

    [Serializable()]
    internal class HeaderTextSettings
    {

        public string Text { get { return this._text; } set { this._text = value; } }
        public string Font { get { return this._font; } set { this._font = value; } }
        public string Bc { get { return this._bc; } set { this._bc = value; } }
        public string Fc { get { return this._fc; } set { this._fc = value; } }

        private string _text = string.Empty;
        private string _font = string.Empty;
        private string _bc = string.Empty;
        private string _fc = string.Empty;
    }
}
